package rahmath.sharif.docrypt;

import rahmath.sharif.docrypt.constant.DocryptConstants;
import rahmath.sharif.docrypt.util.AESCryptor;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class DocryptDrivePasswordActivity extends Activity implements
		DocryptConstants {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_docrypt_drive_password);
	}

	public void save(View view) {
		String drivePassword = ((EditText) findViewById(R.id.drivePassword))
				.getText().toString();
		String confirmDrivePassword = ((EditText) findViewById(R.id.confirmDrivePassword))
				.getText().toString();
		if (drivePassword.equals(confirmDrivePassword)) {
			Editor editor = getSharedPreferences("Docrypt", MODE_PRIVATE)
					.edit();
			editor.putString(DOCRYPT_DRIVE_PASSWORD,
					AESCryptor.encryptPassword(256, drivePassword));
			editor.commit();
			finish();
		} else {
			Toast.makeText(DocryptDrivePasswordActivity.this,
					getString(R.string.passwordsNotMatch), Toast.LENGTH_SHORT)
					.show();
		}
	}

	public void cancel(View view) {
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_docrypt_drive_password, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.help:
			Intent helpActivity = new Intent(DocryptDrivePasswordActivity.this,
					HelpActivity.class);
			startActivity(helpActivity);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
